package com.tbit.uqbike.webmanager.business.brandshare.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.config.datasource.DataSource;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.webmanager.business.brandshare.dao.core.BrandShareConfigDao;
import com.tbit.uqbike.webmanager.business.brandshare.pojo.BrandShareConfig;
import com.tbit.uqbike.webmanager.business.brandshare.pojo.BrandShareRelation;
import com.tbit.uqbike.webmanager.business.brandshare.pojo.vo.BrandShareConfigAddVO;
import com.tbit.uqbike.webmanager.business.brandshare.pojo.vo.BrandShareConfigUpdateVO;
import com.tbit.uqbike.webmanager.business.brandshare.pojo.vo.BrandShareListReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌共享配置服务类
 *
 * @author: qiu.longjie
 * @create: 2025-05-19
 */
@Slf4j
@Service
public class BrandShareConfigService extends ServiceImpl<BrandShareConfigDao, BrandShareConfig> {

    @Autowired
    BrandShareRelationService brandShareRelationService;

    public List<BrandShareConfig> queryList(BrandShareListReqVo req) {
        return baseMapper.queryList(req.getBrandId(), req.getConfigName());
    }

    @Transactional
    @DS(DataSource.CORE)
    public void addConfig(BrandShareConfigAddVO vo) {
        if (vo.getBrandIdList() == null || vo.getBrandIdList().size() < 2){
            throw new BaseException("品牌不能少于两个");
        }
        log.info("新增品牌共享配置,vo:{}", vo);
        BrandShareConfig brandShareConfig = new BrandShareConfig();
        brandShareConfig.setConfigName(vo.getConfigName());
        save(brandShareConfig);
        saveBrandShareRelations(vo.getBrandIdList(), brandShareConfig.getId());
    }

    @Transactional
    @DS(DataSource.CORE)
    public void updateConfig(BrandShareConfigUpdateVO vo) {
        if (vo.getId() == null){
            throw new BaseException("配置ID不能为空");
        }
        if (vo.getBrandIdList() == null || vo.getBrandIdList().size() < 2){
            throw new BaseException("品牌不能少于两个");
        }
        // 获取配置信息
        BrandShareConfig brandShareConfig = getById(vo.getId());
        if (brandShareConfig == null){
            throw new BaseException("配置不存在");
        }
        brandShareConfig.setConfigName(vo.getConfigName());
        // 简单点 - 先删除后再新增 数据不会有多少
        log.info("更新品牌共享配置,vo:{}", vo);
        updateById(brandShareConfig);
        brandShareRelationService.deleteByConfigId(vo.getId());
        saveBrandShareRelations(vo.getBrandIdList(), vo.getId());
    }

    @Transactional
    @DS(DataSource.CORE)
    public void deleteConfig(Integer configId) {
        if (configId == null){
            throw new BaseException("配置ID不能为空");
        }
        log.info("删除品牌共享配置,配置ID:{}", configId);
        removeById(configId);
        brandShareRelationService.deleteByConfigId(configId);
    }

    private void saveBrandShareRelations(List<Integer> brandList, Integer configId) {
        List<BrandShareRelation> brandShareRelations = new ArrayList<>(brandList.size());
        brandList.forEach(brandId -> {
            brandShareRelations.add(new BrandShareRelation().setBrandId(brandId).setConfigId(configId));
        });
        brandShareRelationService.saveBatch(brandShareRelations);
    }
}
